// pages/updatedetail/updatedetail.js
const { phoneApi, versionApi, downloadApi } = require('../../utils/api');

Page({
  data: {
    phoneDetail: null,
    versionDetail: null,
    versionId: null,
    phoneId: null,
    downloadLinks: {
      card: [],
      line: []
    }
  },

  onLoad: function (options) {
    const versionId = parseInt(options.versionId);
    const phoneId = parseInt(options.phoneId);
    
    this.setData({ 
      versionId: versionId,
      phoneId: phoneId
    });
    
    // 获取手机详情
    this.loadPhoneInfo(phoneId);
    
    // 获取版本详情
    this.loadVersionInfo(versionId);
    
    // 获取下载链接
    this.loadDownloadLinks(versionId);
  },
  
  // 加载手机详情
  loadPhoneInfo: function(phoneId) {
    phoneApi.getPhoneDetail(phoneId).then(data => {
      this.setData({ phoneDetail: data });
    }).catch(err => {
      console.error('获取手机详情失败', err);
      this.showErrorAndGoBack();
    });
  },
  
  // 加载版本详情
  loadVersionInfo: function(versionId) {
    versionApi.getVersionDetail(versionId).then(data => {
      this.setData({ versionDetail: data });
    }).catch(err => {
      console.error('获取版本详情失败', err);
      this.showErrorAndGoBack();
    });
  },
  
  // 加载下载链接
  loadDownloadLinks: function(versionId) {
    downloadApi.getDownloadLinks(versionId).then(data => {
      this.setData({ downloadLinks: data || { card: [], line: [] } });
    }).catch(err => {
      console.error('获取下载链接失败', err);
      this.setData({ downloadLinks: { card: [], line: [] } });
    });
  },
  
  // 显示错误并返回
  showErrorAndGoBack: function() {
    wx.showToast({
      title: '信息不存在',
      icon: 'none'
    });
    setTimeout(() => {
      wx.navigateBack();
    }, 1500);
  },

  // 复制下载链接
  copyLink: function(e) {
    const url = e.currentTarget.dataset.url;
    wx.setClipboardData({
      data: url,
      success: function() {
        wx.showToast({
          title: '下载链接已复制',
          icon: 'success'
        });
      }
    });
  }
}) 